package com.aidong.dynamic.biz.impl;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.aidong.dynamic.biz.TrnDynamicContentBiz;
import com.aidong.dynamic.dao.TrnDynamicContentDao;
import com.aidong.dynamic.entity.TrnDynamicContent;
import com.aidong.login.dao.MemberInformationDao;
import com.aidong.util.ImageUpload;

/**
 * 运动圈动态service
 * @author java
 * @version 1.0    
 *  2015.6.2
 */
@Service("trnDynamicContentBiz")
public class TrnDynamicContentBizImpl implements TrnDynamicContentBiz {
	
	private TrnDynamicContentDao trnDynamicContentDao;
	
	private MemberInformationDao memberInformationDao;

	@Autowired
	public void setMemberInformationDao(MemberInformationDao memberInformationDao) {
		this.memberInformationDao = memberInformationDao;
	}

	@Autowired
	public void trnDynamicContentDao(TrnDynamicContentDao trnDynamicContentDao) {
		this.trnDynamicContentDao = trnDynamicContentDao;
	}
	
	/**
	 * 查找运动圈动态
	 */
	@Override
	public List<Map<String, Object>> getTrnDynamic(Map<String, Object> map) {
		return trnDynamicContentDao.getTrnDynamic(map);
	}

	/**
	 * 查找此条动态是否被所登录的用户点过赞
	 * @param userId
	 * @return
	 */
	@Override
	public String getIfTrnPraise(Map<String, Object> map) {
		return trnDynamicContentDao.getIfTrnPraise(map);
	}

	/**
	 * 发表运动圈动态
	 * @param map
	 */
	@Override
	public void addTrnDynamic(TrnDynamicContent dynamicContent) {
		 Date date = new Date(); // 得到系统时间
         SimpleDateFormat format = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
         String time = format.format(date);
         dynamicContent.setPublishTime(time); // 发布时间
         
         if(dynamicContent.getPicture() != null && !"".equals(dynamicContent.getPicture())){
        	 dynamicContent.setPicture(ImageUpload.readBlob(dynamicContent.getPicture(),"circles")); // 图片
         }
		trnDynamicContentDao.addTrnDynamic(dynamicContent);
		// 增加热度
		Map<String, Object> paraMap = new HashMap<String, Object>();
		paraMap.put("idongId", dynamicContent.getIdongId());
		memberInformationDao.addMemberPopty(paraMap);
	}

	/**
	 * 运动圈背景图
	 * @param idongId
	 * @return
	 */
	@Override
	public Map<String, Object> getDynamicBackImage(int idongId) {
		return trnDynamicContentDao.getDynamicBackImage(idongId);
	}

	/**
	 * 运动圈背景图修改
	 * @param map
	 */
	@Override
	public void editDynamicBackImage(Map<String, Object> map) {
		String savePath = ImageUpload.readBlob(map.get("backUrl") + "", "friends");
		map.put("backUrl", savePath);
		trnDynamicContentDao.editDynamicBackImage(map);
	}

	/**
	 * 运动圈最热
	 * @param map
	 * @return
	 */
	@Override
	public List<Map<String, Object>> getDynamicHot(Map<String, Object> map) {
		return trnDynamicContentDao.getDynamicHot(map);
	}
	/**
	 * 查找某人最新的三条运动圈
	 * @param map
	 * @return
	 */
	@Override
	public List<Map<String, Object>> getPersonDynamicTopThree(Map<String, Object> map) {
		// TODO Auto-generated method stub
		return trnDynamicContentDao.getPersonDynamicTopThree(map);
	}

	@Override
	public void addReportDynamic(Map<String, Object> map) {
		// TODO Auto-generated method stub
		trnDynamicContentDao.addReportDynamic(map);
	}
}
